﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using SchemaMonitor;
using SchemaMonitor.json;
using Framework;

public partial class pages_chapters_chapter : System.Web.UI.Page
{

	public int ChapterId { get { return CWeb.RequestInt("chapterId"); } }
	public Guid NodeGuid { get { return CWeb.RequestGuid("nodeGuid", CNodeRoot.Str2Guid(CWeb.RequestStr("id"))); } }

	public CChapter Chapter { get { return CChapter.Cache.GetById(ChapterId); } }

	protected void Page_Init(object sender, EventArgs e)
	{
		lnkBack.NavigateUrl = null == Chapter ? "~/?menu=1" : CSitemap.SectionEdit(Chapter.Section);
		CDropdown.Add(rblRating, "Unrated", 0);
		CDropdown.Add(rblRating, CMetaTag.Cache.Crim.TagName, CMetaTag.Cache.Crim.TagId);
		CDropdown.Add(rblRating, CMetaTag.Cache.Low.TagName, CMetaTag.Cache.Low.TagId);
		CDropdown.Add(rblRating, CMetaTag.Cache.Med.TagName, CMetaTag.Cache.Med.TagId);
		CDropdown.Add(rblRating, CMetaTag.Cache.High.TagName, CMetaTag.Cache.High.TagId);
		CDropdown.Add(rblRating, CMetaTag.Cache.Feat.TagName, CMetaTag.Cache.Feat.TagId);

		var c = Chapter;
		CDropdown.SetValue(rblRating, c.RatingTagId);

		lnkmages.NavigateUrl = CSitemap.PhotoHash(c.ChapterId);

		cblTags.DataSource = CTagType.Cache.FB_Post.MetaTags;
		cblTags.DataBind();

		var n = null == c ? CSession.User.All.GetById(NodeGuid) : c.Node_(CSession.User);
		CDropdown.SetValues(cblTags, n.NodeLabels.MetatagIds);

		CTextbox.OnReturnPress(txtNewTag, btnAddTag);


		var r = n.Root;
		var p = new CSoftPost(r, CSession.User);
		if (null != c && c.ChapterPictures.Count > 0)
			ctrlPhotos.Display(c.ChapterPictures.Photos);
		else if (p.HasAttachments)
			ctrlPhotos.Display(p.Attachments_);
		else
			ctrlPhotos.Visible = false;
	}
	protected void Page_Load(object sender, EventArgs e)
	{
		var c = Chapter;
		var n = null == c ? CSession.User.All.GetById(NodeGuid) : c.Node_(CSession.User);
		var r = n.Root;
		var p = new CSoftPost(r, CSession.User);

		if (null == c)
		{
			rblRating.Visible = false;
			return;
		}
		lblId.Text = r.Id;
		lblObjectId.Text = p.ObjectId;
		lblObjectId.NavigateUrl = "~/?typeId=149&view=4&nodeId=" + p.Root_.Id;
		lblId.NavigateUrl = lblObjectId.NavigateUrl;
		lblCreated.Text = CUtilities.Timespan(p.CreatedTime);
		lblUpdated.Text = CUtilities.Timespan(p.UpdatedTime);
		if (null != p.From)	
			lblFrom.Text = p.From.Name;
		lblStatusType.Text = p.StatusType;
		lblStory.Text = p.Story;
		lblPrivacy.Text = p.Privacy;
		imgPicture.ImageUrl = p.Picture;
		imgPicture.Visible = !string.IsNullOrEmpty(p.Picture);
		imgFullPic.ImageUrl = p.FullPicture;
		imgFullPic.Visible = !string.IsNullOrEmpty(p.FullPicture);
		lblMessage.Text = HttpUtility.HtmlEncode(p.Message).Replace("\r\n", "<br/").Replace("\n", "<br/>");

		ctrlComments.Display(p.Comments);

	}

	protected void rblRating_SelectedIndexChanged(object sender, EventArgs e)
	{
		Chapter.RatingTagId = CDropdown.GetInt(rblRating);
		//Chapter.Save();
		Response.Redirect(Request.RawUrl);
	}

	protected void btnSaveTags_Click(object sender, EventArgs e)
	{

		var c = Chapter;
		var n = null == c ? CSession.User.All.GetById(NodeGuid) : c.Node_(CSession.User);

		var newTagIds = CDropdown.SelectedInts(cblTags);
		var oldTagIds = n.NodeLabels.MetatagIds;
		foreach (var i in oldTagIds)
			if (!newTagIds.Contains(i))
			{
				var lbl = CNodeLabel.Cache.GetById(n.NodeGuid, i);
				if (null != lbl)
					lbl.Delete();
			}
		foreach (var i in newTagIds)
			if (!oldTagIds.Contains(i))
			{
				var lbl = new CNodeLabel();
				lbl.LabelNodeGuid = n.NodeGuid;
				lbl.LabelTagId = i;
				lbl.Save();
			}

		Response.Redirect(Request.RawUrl);
	}

	protected void btnAddTag_Click(object sender, EventArgs e)
	{
		var s = txtNewTag.Text;
		if (s.Length == 0)
			return;
		var t = new CMetaTag();
		t.TagName = s;
		t.TagTypeId = CTagType.Cache.FB_Post.TypeId;
		t.Save();

		var lbl = new CNodeLabel();
		lbl.LabelNodeGuid = Chapter.ChapterNodeGuid;
		lbl.LabelTagId = t.TagId;
		lbl.Save();

		Response.Redirect(Request.RawUrl);
	}
}